package com.example.demo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by wang.lin@esstx.cn on 2018/4/20.
 */
public class ObjectKit {
    private static final Logger logger = LoggerFactory.getLogger(ObjectKit.class);

    private ObjectKit() {
    }
    private static final Pattern NUMBER_PATTERN = Pattern.compile("^[-\\+]?[\\d]*$");
    /**
     * 验证是否是数字
     */
    public static boolean isInteger(String str) {

        return NUMBER_PATTERN.matcher(str).matches();
    }

    public static boolean isEmpty(Object object) {
        return object == null || object.equals("") || object.equals("null");
    }

    public static boolean isNotEmpty(Object object) {
        return !isEmpty(object);
    }
    public static boolean listNotNull(List list){
        return list!=null && !list.isEmpty();
    }
    /**
     * 获取JavaBean风格的属性值
     */
    public static Object get(Object o , String propertyName){
        try {
            propertyName = StrKit.toUpperCaseFirstOne(propertyName);
            Class<?> clazz = o.getClass();
            Method getMethod = clazz.getMethod("get" + propertyName);
            return getMethod.invoke(o);
        } catch (Exception e) {
            logger.error(e.getMessage() , e);
        }
        return null;
    }

    public static Object getRecordValue(Object o , String propertyName){
        try {
            Class<?> clazz = o.getClass();
            Method getMethod = clazz.getMethod("get",String.class);
            return getMethod.invoke(o,propertyName);
        } catch (Exception e) {
            logger.error(e.getMessage() , e);
        }
        return null;
    }

    public static String  toString(Object object) {
        return object!=null?object.toString():null;
    }

    public static Float toFloat(Object object){
        return object!=null?Float.parseFloat(object.toString()):null;
    }

    public static Double toDouble(Object object){
        java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");
        return object!=null?Double.valueOf(df.format(object)):null;
    }

    public static Integer toInt(Object object){
        return object!=null?Integer.parseInt(object.toString()):null;
    }

    public static Date toDate(Object object) throws ParseException {
        return DateKit.getDate(ObjectKit.toString(object));
    }

    public static boolean canSetValueDirectly(Class<?> clazz){
        return clazz.isPrimitive() // 基本类型
                || clazz == String.class  //String
                || clazz == Character.class //Character
                || clazz == Boolean.class  //Boolean
                || clazz == List.class  //Boolean
                || Number.class.isAssignableFrom(clazz) //Double Float Long Integer Short Byte BigInteger BigDecimal
                || Date.class.isAssignableFrom(clazz) // java.sql.Date,Time,TimeStamp
                ;
    }

    public static <T> void setValue(T instance, Field field , Object v) {
        field.setAccessible(true);
        try {
            v = compatibleValue(v , field.getType());
            field.set(instance,v);
        } catch (IllegalAccessException e) {
            logger.error(e.getMessage() , e);
        }
    }

    /**
     * 将一个值转换为兼容类型的值
     * @param value src
     * @param toClazz 转换的类型
     */
    public static Object compatibleValue(Object value , Class<?> toClazz){
        if(toClazz == Byte.class || toClazz == byte.class){
            if(value instanceof Number){
                return ((Number)value).byteValue();
            }else if(value instanceof String){
                return Byte.valueOf((String) value);
            }
        }
        if(toClazz == Short.class || toClazz == short.class){
            if(value instanceof Number){
                return ((Number)value).shortValue();
            }else if(value instanceof String){
                return Short.valueOf((String) value);
            }
        }
        if(toClazz == Integer.class || toClazz == int.class){
            if(value instanceof Number){
                return ((Number)value).intValue();
            }else if(value instanceof String){
                return Integer.valueOf((String) value);
            }
        }
        if(toClazz == Long.class || toClazz == long.class){
            if(value instanceof Number){
                return ((Number)value).longValue();
            }else if(value instanceof String){
                return Long.valueOf((String) value);
            }
        }
        if(toClazz == Double.class || toClazz == double.class){
            if(value instanceof Number){
                return ((Number)value).doubleValue();
            }else if(value instanceof String){
                return Double.valueOf((String) value);
            }
        }
        if(toClazz == Float.class || toClazz == float.class){
            if(value instanceof Number){
                return ((Number)value).floatValue();
            }else if(value instanceof String){
                return Float.valueOf((String) value);
            }
        }
        if(toClazz == BigDecimal.class){
            return new BigDecimal(value.toString());
        }
        if(toClazz == BigInteger.class){
            return new BigInteger(value.toString());
        }
        if(toClazz == Boolean.class || toClazz == boolean.class){
            return Boolean.valueOf(value.toString());
        }
        if(toClazz == Character.class || toClazz == char.class){
            return value;
        }

        if(toClazz == String.class){
            return value.toString();
        }

        return value;
    }

    /**
     * list转String
     */
    public static String listToString2(List<Integer> mList) {
        final String SEPARATOR = "','";
        StringBuilder sb = new StringBuilder();
        String convertedListStr = "";
        if (null != mList && mList.size() > 0) {
            for (Integer item : mList) {
                sb.append(item);
                sb.append(SEPARATOR);
            }
            convertedListStr = sb.toString();
            convertedListStr = convertedListStr.substring(0, convertedListStr.length()
                    - SEPARATOR.length());
            return convertedListStr;
        }
        return "";
    }
}
